package com.duan.laugh.msg.service.impl;

import cn.hutool.core.util.StrUtil;
import com.duan.laugh.common.event.constants.EventConstant;
import com.duan.laugh.msg.api.constans.MsgConstants;
import com.duan.laugh.msg.api.pojo.entity.MessageLog;
import com.duan.laugh.msg.api.pojo.entity.Msg;
import com.duan.laugh.msg.api.pojo.entity.MsgSend;
import com.duan.laugh.msg.service.IMessageLogService;
import com.duan.laugh.msg.service.ISysMsgTemplateService;
import com.duan.laugh.msg.service.MsgService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author duanjw
 */
@Service
@Slf4j
@AllArgsConstructor
public class MsgServiceImpl implements MsgService {
    private final SimpMessagingTemplate messagingTemplate;

    @Autowired
    private ISysMsgTemplateService sysMsgTemplateService;
    @Autowired
    private IMessageLogService messageLogService;

    /**
     * 给指定用户推送消息
     *
     * @param msg
     */
    @Override
    public void sendMsgToUser(Msg msg) {
        // 接收者
        final String rec = msg.getRec();
        // 消息内容
        final String content = msg.getContent();
        // 发送消息
        messagingTemplate.convertAndSendToUser(rec, MsgConstants.USER_MSG_DESTINATION, content);
        log.info("推送消息成功，接收者：【{}】，消息内容：【{}】", rec, content);
    }

    /**
     * 给所有用户推送消息
     *
     * @param msg
     */
    @Override
    public void sendMsgToAllUser(Msg msg) {
        // 消息内容
        final String content = msg.getContent();
        messagingTemplate.convertAndSend(MsgConstants.TOPIC_SYS, content);
        log.info("给所有用户推送消息成功，消息内容：【{}】", content);
    }

    /**
     * 给指定用户推送站内消息
     *
     * @param msgSend
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMsgToUser(MsgSend msgSend) {
        Long templateId = msgSend.getTemplateId();
//        SysMsgTemplate msgTemplate = null;
        MessageLog messageLog;
        // 接收用户
        List<String> userIds = StrUtil.split(msgSend.getReceiveObjectValue(), ',', -1);
        // 内容(临时)
        String contant = templateId + " " + msgSend.getParams();
        for (int i = 0, n = userIds.size(); i < n; i++) {
            String userId = userIds.get(i);
//            msgTemplate = sysMsgTemplateService.getById(msgSend.getTemplateId());
//            String content = msgTemplate.getContent();
            messagingTemplate.convertAndSendToUser(userId, MsgConstants.USER_MSG_DESTINATION, contant);
            log.info("推送消息成功，接收者：【{}】，消息内容：【{}】", userId, contant);
            // 存入日志
            messageLog = new MessageLog();
            messageLog.setTemplateId(templateId)
                    .setContent(contant)
                    .setCreateTime(LocalDateTime.now())
                    .setReceivePersonId(userId)
                    .setSendTime(LocalDateTime.now())
                    .setStatus(EventConstant.STATUS_UNREAD)
                    .setTenantId(msgSend.getTenantId())
                    .setReceivePersonId(userId)
                    // 临时为 0
                    .setSendPersonId("0");
            messageLogService.save(messageLog);
        }
    }
}
